package com.xiaoliu.tools;

import java.io.File;
import java.util.LinkedHashSet;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JFieldRef;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.xiaoliu.pojo.TableInfoPOJO;


@Component
public class GenerateEntity {

	@Autowired
	private TableInfo info;
	/**
	 * 生成实体
	 * @param tableName 表名
	 * @param aliasName 别名
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	public void getGenerate(String tableName,String aliasName) throws Exception{
		//创建model
		 JCodeModel cm = new JCodeModel(); 
		 //设置类名
		 JType cn=null;
		 if(null==aliasName) {
			 cn = cm.parseType(tableName);
		 }else {
			 cn=cm.parseType(aliasName);
		 }
		 //设置存放路径
		 File file=new File("src/main/java");
		 //设置存放包
		 JDefinedClass dc = cm._class("cn.xiaoliu.entity."+aliasName);
		 //根据表名获得类成员
		LinkedHashSet<TableInfoPOJO> tableInfo = info.getTableInfo(tableName);
		 //定义构造函数
		 dc.constructor(JMod.PRIVATE); 
		 for (TableInfoPOJO pojo : tableInfo) {
			 String type = this.getAbilityType(pojo.getTypeName()).trim();
			//定义成员
			JFieldVar field = dc.field(JMod.PRIVATE, cm.parseType(type), pojo.getColumnName());
			//生成javadoc
			field.javadoc().add(pojo.getRemarks());
			//列名首字母大写
			String name=pojo.getColumnName().substring(0,1).toUpperCase()+pojo.getColumnName().substring(1);
			//定义成员的get方法
			JMethod method = dc.method(JMod.PUBLIC, cm.parseType(type),"get"+name);
			//创建get身体
			JBlock getInstanceBody = method.body(); 
			 JFieldRef fieldRef = JExpr.ref(pojo.getColumnName()); 
			 /*//添加if判断
			  JConditional conditionIf = getInstanceBody._if(fieldRef.eq(JExpr 
					  ._null())); 
			  JBlock thenPart = conditionIf._then(); 
		         thenPart.assign(fieldRef, JExpr._new(cm.parseType(type),)); */
		     getInstanceBody._return(fieldRef); 
		     //定义成员set方法
		     JMethod methodSet = dc.method(JMod.PUBLIC, cm.parseType("void"), "set"+name);
		     //创建set身体
		     JBlock body = methodSet.body();
		     //设置set方法参数
		     methodSet.param(cm.parseType(type), pojo.getColumnName());
		     
		     JFieldRef ihs = JExpr.ref(pojo.getColumnName()); 
		     
		     body.assign(JExpr.refthis(pojo.getColumnName()), ihs);
		}
		 cm.build(file);
	}
	
	/**
	 * 表名
	 * @param tableName
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	public void getGenerate(String tableName) throws Exception{
		//创建model
		 JCodeModel cm = new JCodeModel(); 
		 //设置类名
		 JType cn = cm.parseType(tableName);
		 //设置存放路径
		 File file=new File("src/main/java");
		 //设置存放包
		 JDefinedClass dc = cm._class("cn.allsales.entity."+tableName);
		 //根据表名获得类成员
		LinkedHashSet<TableInfoPOJO> tableInfo = info.getTableInfo(tableName);
		 //定义构造函数
		 dc.constructor(JMod.PRIVATE); 
		 for (TableInfoPOJO pojo : tableInfo) {
			 String type = this.getAbilityType(pojo.getTypeName()).trim();
			//定义成员
			JFieldVar field = dc.field(JMod.PRIVATE, cm.parseType(type), pojo.getColumnName());
			//生成javadoc
			field.javadoc().add(pojo.getRemarks());
			//列名首字母大写
			String name=pojo.getColumnName().substring(0,1).toUpperCase()+pojo.getColumnName().substring(1);
			//定义成员的get方法
			JMethod method = dc.method(JMod.PUBLIC, cm.parseType(type),"get"+name);
			//创建get身体
			JBlock getInstanceBody = method.body(); 
			 JFieldRef fieldRef = JExpr.ref(pojo.getColumnName()); 
			 /*//添加if判断
			  JConditional conditionIf = getInstanceBody._if(fieldRef.eq(JExpr 
					  ._null())); 
			  JBlock thenPart = conditionIf._then(); 
		         thenPart.assign(fieldRef, JExpr._new(cm.parseType(type),)); */
		     getInstanceBody._return(fieldRef); 
		     //定义成员set方法
		     JMethod methodSet = dc.method(JMod.PUBLIC, cm.parseType("void"), "set"+name);
		     //创建set身体
		     JBlock body = methodSet.body();
		     //设置set方法参数
		     methodSet.param(cm.parseType(type), pojo.getColumnName());
		     
		     JFieldRef ihs = JExpr.ref(pojo.getColumnName()); 
		     
		     body.assign(JExpr.refthis(pojo.getColumnName()), ihs);
		}
		 cm.build(file);
	}
	
	/**
	 * 转换数据类型
	 * @param dataBaseType
	 * @return
	 */
	public String getAbilityType(String dataBaseType) {
		//为了保证一致性 一定要忽略大小写
		String lower = dataBaseType.toLowerCase();
		if(lower.contains("varchar") || lower.contains("text")) {
			return "String";
		}else if(lower.contains("int")) {
			return "Integer";
		}else if(lower.contains("serial")) {
			return "Long";
		}else if(lower.contains("double")) {
			return "Double";
		}else if(lower.contains("money")) {
			return "BigDecimal";
		}else if(lower.contains("date")) {
			return "Date";
		}else if(lower.contains("boolean")) {
			return "Boolean";
		}else {
			return "String";
		}
	}

}
